<?php
class C_mandor extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_mandor');
		$this->load->model('m_wakil');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			case 'DETAIL' :
				$this->_detail();
				break;
			case 'DEACTIVATE' :
				$this->_deactivate();
				break;
			default :
				$this->load->view('app/v_mandor');
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['search'] = nosql_post("search");
		$data['mandor_nama'] = nosql_post("mandor_nama");
		$data['mandor_kode'] = nosql_post("mandor_kode");
		$data['mandor_bu'] = nosql_post("mandor_bu");
		$data['mandor_alamat'] = nosql_post("mandor_alamat");
		$data['mandor_npwp'] = nosql_post("mandor_npwp");
		$data['mandor_telpon'] = nosql_post("mandor_telpon");
		$data['mandor_fax'] = nosql_post("mandor_fax");
		$data['mandor_email'] = nosql_post("mandor_email");
		$data['mandor_pengalaman'] = nosql_post("mandor_pengalaman");
		$data['mandor_kemampuan'] = nosql_post("mandor_kemampuan");
		$data['mandor_wakil'] = nosql_post("mandor_wakil");
		$data['mandor_tukang1'] = nosql_post("mandor_tukang1");
		$data['mandor_tukang2'] = nosql_post("mandor_tukang2");
		$data['mandor_pekerja'] = nosql_post("mandor_pekerja");
		$data['m_keuangan_id'] = nosql_post("m_keuangan_id");
		$data['m_pendanaan_id'] = nosql_post("m_pendanaan_id");
		echo $this->m_mandor->_list($data);
	}
	
	function _insert(){
        $mandor_kemampuan = $this->input->post('mandor_kemampuan');
        
        if (! is_array($mandor_kemampuan))
            $mandor_kemampuan = array($mandor_kemampuan);
        
		$data = array(
			'mandor_nama' => $this->input->post('mandor_nama'), 
			'mandor_kode' => $this->input->post('mandor_kode'), 
			'mandor_bu' => $this->input->post('mandor_bu'), 
			'mandor_status' => $this->input->post('mandor_status'), 
			'mandor_kota' => $this->input->post('mandor_kota'), 
			'mandor_alamat' => $this->input->post('mandor_alamat'), 
			'mandor_npwp' => $this->input->post('mandor_npwp'), 
			'mandor_telpon' => $this->input->post('mandor_telpon'), 
			'mandor_fax' => $this->input->post('mandor_fax'), 
			'mandor_email' => $this->input->post('mandor_email'), 
			'mandor_pengalaman' => $this->input->post('mandor_pengalaman'), 
			'mandor_kemampuan' => implode($mandor_kemampuan,","), 
			'mandor_wakil' => $this->input->post('mandor_wakil'), 
			'mandor_tukang1' => $this->input->post('mandor_tukang1'), 
			'mandor_tukang2' => $this->input->post('mandor_tukang2'), 
			'mandor_pekerja' => $this->input->post('mandor_pekerja'), 
			'm_keuangan_id' => $this->input->post('m_keuangan_id'), 
			'm_pendanaan_id' => $this->input->post('m_pendanaan_id'), 
			  
			'mandor_insert_user'	=>$_SESSION[SESSION_USERID],
			'mandor_insert_date'=> date("Y-m-d h:i:s"));
		$mandor_id = $this->m_mandor->_insert($data);
        
        $this->_insertdetail($mandor_id);
		echo 1;
	}
	
	function _update(){
		$mandor_id = $this->input->post('mandor_id');
        $mandor_kemampuan = $this->input->post('mandor_kemampuan');
        
        if (! is_array($mandor_kemampuan))
            $mandor_kemampuan = array($mandor_kemampuan);
        
		$data = array(
			'mandor_nama' => $this->input->post('mandor_nama'), 
			'mandor_kode' => $this->input->post('mandor_kode'), 
			'mandor_bu' => $this->input->post('mandor_bu'), 
			'mandor_status' => $this->input->post('mandor_status'), 
			'mandor_kota' => $this->input->post('mandor_kota'), 
			'mandor_alamat' => $this->input->post('mandor_alamat'), 
			'mandor_npwp' => $this->input->post('mandor_npwp'), 
			'mandor_telpon' => $this->input->post('mandor_telpon'), 
			'mandor_fax' => $this->input->post('mandor_fax'), 
			'mandor_email' => $this->input->post('mandor_email'), 
			'mandor_pengalaman' => $this->input->post('mandor_pengalaman'), 
			'mandor_kemampuan' => implode($mandor_kemampuan,","), 
			'mandor_wakil' => $this->input->post('mandor_wakil'), 
			'mandor_tukang1' => $this->input->post('mandor_tukang1'), 
			'mandor_tukang2' => $this->input->post('mandor_tukang2'), 
			'mandor_pekerja' => $this->input->post('mandor_pekerja'), 
			'm_keuangan_id' => $this->input->post('m_keuangan_id'), 
			'm_pendanaan_id' => $this->input->post('m_pendanaan_id'), 
			
			'mandor_update_user'	=> $_SESSION[SESSION_USERID],
			'mandor_update_date'=> date("Y-m-d h:i:s"));
		$this->m_mandor->_update($data,$mandor_id);
        
        $this->_deletedetail($mandor_id);        
        $this->_insertdetail($mandor_id);
        
		echo 1;
 	}
	
	function _delete(){
		$mandor_id= $this->input->post('mandor_id');
		$this->m_mandor->_delete($mandor_id);
		echo 1;
	}
	
	function _deactivate(){
		$mandor_id= $this->input->post('mandor_id');
		$data = array(
			'mandor_active' => 'n'
		);
		$this->m_mandor->_deactivate($data,$mandor_id);
		echo 1;
	}
    
    function _insertdetail($mandor_id){
        
        $wakil_nama = $this->input->post('wakil_nama');
        $wakil_telpon = $this->input->post('wakil_telpon');
        
        if ( $wakil_nama != ""){
                
            if (! is_array($wakil_nama)){
                $wakil_nama = array($wakil_nama);
                $wakil_telpon = array($wakil_telpon);
            }
            
            for($i=0;$i<count($wakil_nama);$i++){
            
                $data = array(
                    'm_mandor_id' => $mandor_id, 
                    'wakil_nama' => $wakil_nama[$i], 
                    'wakil_telpon' => $wakil_telpon[$i], 
                      
                    'wakil_insert_user'	=>$_SESSION[SESSION_USERID],
                    'wakil_insert_date'=> date("Y-m-d h:i:s"));
                $this->m_wakil->_insert($data);
            }
        }
    }
    
    function _deletedetail($mandor_id){
        $this->m_wakil->_deletebymandor($mandor_id);
    }
    
    function _detail(){
        $mandor_id = secure_post("mandor_id");
        echo json_encode($this->m_wakil->_listbymandor($mandor_id));
    }
}